package com.bluetooth.temp.util;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class DataUtil {

    /**
     * 解析温度
     */
    public static float parseTempValue(String hex) {
        try {
            //"A5(头) 1F85AF41 8FC2E342 0A(校验和) 5A(尾)";
            //header + 数据 + 校验和 + footer
            String str1 = hex.substring(2, hex.length()); //去头
            String str2 = str1.substring(0, str1.length() - 4); //去尾
            String data = str2.substring(0, 8); //0-8位是温度

            //1F85AF41 反转为：41AF851F
            //反转十六进制值, 转换为float类型，即为真实数据
            StringBuilder result = new StringBuilder();
            for (int i = 0; i <= data.length() - 2; i = i + 2) {
                result.append(new StringBuilder(data.substring(i, i + 2)).reverse());
            }

            String newHex = result.reverse().toString();
            return hexToFloat(newHex);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0f;
    }

    /**
     * 解析湿度
     */
    public static float parseHumValue(String hex) {
        try {
            //"A5 1F85AF41 8FC2E342 0A5A";
            //header + 数据 + 校验和 + footer
            String str1 = hex.substring(2, hex.length()); //去头
            String str2 = str1.substring(0, str1.length() - 4); //去尾
            String data = str2.substring(8, 16); //8-16位是湿度

            //反转hex
            StringBuilder result = new StringBuilder();
            for (int i = 0; i <= data.length() - 2; i = i + 2) {
                result.append(new StringBuilder(data.substring(i, i + 2)).reverse());
            }

            String newHex = result.reverse().toString();
            return hexToFloat(newHex);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0f;
    }

    public static String byte2Hex(byte[] data) {
        final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
        if (data == null) {
            return "";
        }
        final int length = data.length;
        if (length == 0) {
            return "";
        }

        char[] hexChars = new char[data.length * 2];
        for (int j = 0; j < data.length; j++) {
            int v = data[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static int hex2Int(String hex) {
        try {
            //1900000000,去掉16进制数末尾0
            hex = hex.replaceAll("(0)+$", "");
            return Integer.parseInt(hex, 16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     *
     * @param hexStr Byte字符串(Byte之间无分隔符
     * @return 对应的字符串
     * @author xxs
     */
    public static String hexToString(String hexStr) {
        String str = "0123456789ABCDEF"; //16进制能用到的所有字符 0-15
        char[] hexs = hexStr.toCharArray();//toCharArray() 方法将字符串转换为字符数组。
        int length = (hexStr.length() / 2);//1个byte数值 -> 两个16进制字符
        byte[] bytes = new byte[length];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            int position = i * 2;//两个16进制字符 -> 1个byte数值
            n = str.indexOf(hexs[position]) * 16;
            n += str.indexOf(hexs[position + 1]);
            // 保持二进制补码的一致性 因为byte类型字符是8bit的  而int为32bit 会自动补齐高位1  所以与上0xFF之后可以保持高位一致性
            //当byte要转化为int的时候，高的24位必然会补1，这样，其二进制补码其实已经不一致了，&0xff可以将高的24位置为0，低8位保持原样，这样做的目的就是为了保证二进制数据的一致性。
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    public static float hexToFloat(String hex) {
        try {
            Integer integer = Integer.valueOf(hex, 16);
            Float f = Float.intBitsToFloat(integer);

            return f.floatValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0f;
    }

    /**
     * 跳转到浏览器
     */
    public static void openBrowser(Context context, String url) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
